{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### ❇️ Pandas 🐼 groupby"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day</th>\n",
       "      <th>city</th>\n",
       "      <th>temperature</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>event</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1/3/2022</td>\n",
       "      <td>new york</td>\n",
       "      <td>28</td>\n",
       "      <td>12</td>\n",
       "      <td>Snow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1/3/2022</td>\n",
       "      <td>mumbai</td>\n",
       "      <td>87</td>\n",
       "      <td>15</td>\n",
       "      <td>Fog</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        day      city  temperature  windspeed event\n",
       "0  1/3/2022  new york           28         12  Snow\n",
       "1  1/3/2022    mumbai           87         15   Fog"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv('weather_data.csv')\n",
    "df.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mumbai\n",
      "        day    city  temperature  windspeed event\n",
      "1  1/3/2022  mumbai           87         15   Fog\n",
      "3  1/4/2022  mumbai           92          5  Rain\n",
      "_____________________________________________________\n",
      "new york\n",
      "        day      city  temperature  windspeed  event\n",
      "0  1/3/2022  new york           28         12   Snow\n",
      "2  1/4/2022  new york           33          7  Sunny\n",
      "_____________________________________________________\n",
      "paris\n",
      "        day   city  temperature  windspeed   event\n",
      "4  1/1/2022  paris           45         20   Sunny\n",
      "5  1/2/2022  paris           50         13  Cloudy\n",
      "6  1/3/2022  paris           54          8  Cloudy\n",
      "_____________________________________________________\n"
     ]
    }
   ],
   "source": [
    "# Let's group the data based on city\n",
    "for city, city_df in df.groupby('city'):\n",
    "    print(city)\n",
    "    print(city_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>day</th>\n",
       "      <th>temperature</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>event</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>mumbai</th>\n",
       "      <td>1/4/2022</td>\n",
       "      <td>92</td>\n",
       "      <td>15</td>\n",
       "      <td>Rain</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>new york</th>\n",
       "      <td>1/4/2022</td>\n",
       "      <td>33</td>\n",
       "      <td>12</td>\n",
       "      <td>Sunny</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>paris</th>\n",
       "      <td>1/3/2022</td>\n",
       "      <td>54</td>\n",
       "      <td>20</td>\n",
       "      <td>Sunny</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               day  temperature  windspeed  event\n",
       "city                                             \n",
       "mumbai    1/4/2022           92         15   Rain\n",
       "new york  1/4/2022           33         12  Sunny\n",
       "paris     1/3/2022           54         20  Sunny"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Now let's check the max temperature and windspeed in each city, you can also try groups.mean()\n",
    "df.groupby('city').max() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"8\" halign=\"left\">temperature</th>\n",
       "      <th colspan=\"8\" halign=\"left\">windspeed</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "      <th>count</th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>min</th>\n",
       "      <th>25%</th>\n",
       "      <th>50%</th>\n",
       "      <th>75%</th>\n",
       "      <th>max</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>city</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>mumbai</th>\n",
       "      <td>2.0</td>\n",
       "      <td>89.500000</td>\n",
       "      <td>3.535534</td>\n",
       "      <td>87.0</td>\n",
       "      <td>88.25</td>\n",
       "      <td>89.5</td>\n",
       "      <td>90.75</td>\n",
       "      <td>92.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>7.071068</td>\n",
       "      <td>5.0</td>\n",
       "      <td>7.50</td>\n",
       "      <td>10.0</td>\n",
       "      <td>12.50</td>\n",
       "      <td>15.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>new york</th>\n",
       "      <td>2.0</td>\n",
       "      <td>30.500000</td>\n",
       "      <td>3.535534</td>\n",
       "      <td>28.0</td>\n",
       "      <td>29.25</td>\n",
       "      <td>30.5</td>\n",
       "      <td>31.75</td>\n",
       "      <td>33.0</td>\n",
       "      <td>2.0</td>\n",
       "      <td>9.500000</td>\n",
       "      <td>3.535534</td>\n",
       "      <td>7.0</td>\n",
       "      <td>8.25</td>\n",
       "      <td>9.5</td>\n",
       "      <td>10.75</td>\n",
       "      <td>12.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>paris</th>\n",
       "      <td>3.0</td>\n",
       "      <td>49.666667</td>\n",
       "      <td>4.509250</td>\n",
       "      <td>45.0</td>\n",
       "      <td>47.50</td>\n",
       "      <td>50.0</td>\n",
       "      <td>52.00</td>\n",
       "      <td>54.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>13.666667</td>\n",
       "      <td>6.027714</td>\n",
       "      <td>8.0</td>\n",
       "      <td>10.50</td>\n",
       "      <td>13.0</td>\n",
       "      <td>16.50</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         temperature                                                       \\\n",
       "               count       mean       std   min    25%   50%    75%   max   \n",
       "city                                                                        \n",
       "mumbai           2.0  89.500000  3.535534  87.0  88.25  89.5  90.75  92.0   \n",
       "new york         2.0  30.500000  3.535534  28.0  29.25  30.5  31.75  33.0   \n",
       "paris            3.0  49.666667  4.509250  45.0  47.50  50.0  52.00  54.0   \n",
       "\n",
       "         windspeed                                                      \n",
       "             count       mean       std  min    25%   50%    75%   max  \n",
       "city                                                                    \n",
       "mumbai         2.0  10.000000  7.071068  5.0   7.50  10.0  12.50  15.0  \n",
       "new york       2.0   9.500000  3.535534  7.0   8.25   9.5  10.75  12.0  \n",
       "paris          3.0  13.666667  6.027714  8.0  10.50  13.0  16.50  20.0  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 💫 getting all the analytics in one go 💥 \n",
    "df.groupby('city').describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
